package com.jeesite.modules.billconfig.web;

import java.util.List;

import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.billconfig.entity.qo.BillConfigQO;
import com.jeesite.modules.billconfig.entity.vo.BillConfigVO;
import com.jeesite.modules.form.service.FormService;
import com.jeesite.modules.sys.utils.UserUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.billconfig.entity.BillConfig;
import com.jeesite.modules.billconfig.service.BillConfigService;

/**
 * 单据配置Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/billconfig/billConfig")
public class BillConfigController extends BaseController {

	@Autowired
	private BillConfigService billConfigService;
	@Autowired
	private FormService formService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public BillConfig get(Long autoId, boolean isNewRecord) {
		return billConfigService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("billconfig:billConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(BillConfig billConfig, Model model) {
		model.addAttribute("billConfig", billConfig);
		model.addAttribute("formList", formService.selectFormList());
		return "modules/billconfig/billConfigList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("billconfig:billConfig:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<BillConfigVO> listData(BillConfigQO billConfigQO, HttpServletRequest request, HttpServletResponse response) {
		billConfigQO.setPage(new Page<>(request, response));
		Page<BillConfigVO> page = billConfigService.findBillConfigPage(billConfigQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("billconfig:billConfig:view")
	@RequestMapping(value = "form")
	public String form(BillConfig billConfig, Model model) {
		if (OrgUtil.isValid()){
			billConfig.setOrgId(OrgUtil.getOrgId());
			billConfig.setOrgCode(OrgUtil.getOrgCode());
			billConfig.setOrgName(OrgUtil.getOrgName());
			billConfig.setCreateName(UserUtils.getUser().getUserName());
			billConfig.setUpdateName(UserUtils.getUser().getUserName());
		}
		model.addAttribute("billConfig", billConfig);
		model.addAttribute("formList", formService.selectFormList());
		return "modules/billconfig/billConfigForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("billconfig:billConfig:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated BillConfig billConfig) {
		billConfigService.save(billConfig);
		return renderResult(Global.TRUE, text("保存单据配置成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("billconfig:billConfig:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(BillConfig billConfig) {
		billConfigService.delete(billConfig);
		return renderResult(Global.TRUE, text("删除单据配置成功！"));
	}
	
}